﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Baimasoft.FrameWork.Base;
using Baimasoft.Manage.SqlServerDAL;

public partial class BBS_Articles_List : Baimasoft.Web.UI.PageManagerTop 
{
    CommClass MyCC = new CommClass();
    user myUser = new user();
    Articles MyAT = new Articles();
    Replies MyRl = new Replies();
    public static string Name = "", TheMaster, Author, RID, RePerson;
    public string SID = "", SName = "", AID = "", AName = "";
    protected string str1, CurrPageIndex;

    DataTable MyTb;

    protected void Page_Load(object sender, EventArgs e)
    {
        try
        {
            AID = Request.QueryString["ArticleID"].ToString().Trim();
        }
        catch
        {
            Response.Write("参数传递错误！");
            Response.End();
        }
        if (AID == "")
        {
            ClientScript.RegisterStartupScript(this.GetType(), "", "<script>alert('参数传递错误！请返回上一页后再次打开该页面！');</script>");
            return;
        }
        try
        {
            SID = Request.QueryString["SectionID"].ToString().Trim();
            SName = Request.QueryString["SectionName"].ToString().Trim();
            AName = MyCC.FilterBadWords(Request.QueryString["ArticleName"].ToString().Trim());
        }
        catch
        {

            Response.Write("参数传递错误！");
            Response.End();
        }
        if (!Page.IsPostBack)
        {
            GridView2.Attributes.Add("BorderColor", "#ffffff");
            GridView1.Attributes.Add("BorderColor", "#ffffff"); 
            //取本版式块版主名称
            TheMaster = myUser.GetMasterUser(SID);
            //取本主题帖子作者
            Author = myUser.GetArticleAuthor(AID);
            //帖子点击数自加1
            myUser.PotsAddone(AID);

            ArticleDataBind();
            RepliesDataBind();

            
        }
    }

    public void ArticleDataBind()
    {
        GridView2.DataSource = MyAT.GetArticleInfo(AID);
        GridView2.DataBind();
    }

    //绑定讨论组的数据源
    public void RepliesDataBind()
    {
        MyTb = MyRl.GetRepliesInfo(AID);
        GridView1.DataSource = MyTb;
        GridView1.DataBind();       
    }

    protected void GridView2_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        try
        {
            if (e.CommandName == "Delete")
            {
                string AId = "0";
                try
                {
                    AId = e.CommandArgument.ToString().Trim();
                }
                catch
                {
                    AId = "0";
                }

                if (((string)Session["Baimasoft_User_LogonID"] == Author) || (string)Session["Baimasoft_User_LogonID"] == TheMaster || (string)Session["Baimasoft_User_LogonID"] == "admin")
                {
                    int iback = MyAT.DeleteArticlesInfo(AId);
                    if (iback > 0)
                    {
                        ClientScript.RegisterStartupScript(this.GetType(), "", "<script>alert('删除成功！');</script>");
                        ArticleDataBind();
                        return;
                    }
                    else
                    {
                        ClientScript.RegisterStartupScript(this.GetType(), "", "<script>alert('删除失败！');</script>");
                        return;
                    }
                }
                else
                {
                    ClientScript.RegisterStartupScript(this.GetType(), "", "<script>alert('对不起，您无此权限！');</script>");
                    return;
                }
            }
            //编辑记录
            if (e.CommandName == "Edit")
            {
                string AId = "0";
                try
                {
                    AId = e.CommandArgument.ToString().Trim();
                }
                catch
                {
                    AId = "0";
                }

                if (((string)Session["Baimasoft_User_LogonID"] == Author) || (string)Session["Baimasoft_User_LogonID"] == TheMaster || (string)Session["Baimasoft_User_LogonID"] == "admin")
                {
                    Response.Redirect("NewTopic_Edit.aspx?SectionID=" + SID + "&SectionName=" + SName + "&ArticleID=" + AID + "&ArticleName=" + AName );
                }
                else
                {
                    ClientScript.RegisterStartupScript(this.GetType(), "", "<script>alert('对不起，您无此权限！');</script>");
                    return;
                }
            }
        }
        catch
        {
            ClientScript.RegisterStartupScript(this.GetType(), "", "<script>alert('对不起，您无此权限！');</script>");
            return;
        }
    }


    protected void GridView2_RowEditing(object sender, GridViewEditEventArgs e)
    {

    }
    protected void GridView2_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {

    }

    protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        try
        {
            if (e.CommandName == "Delete")
            {
                string RId = "0";
                try
                {
                    RId = e.CommandArgument.ToString().Trim();
                }
                catch
                {
                    RId = "0";
                }

                string sReArticleAuthor = myUser.GetReArticleAuthor(RId);  //当前回复者
                string UserName = (string)Session["Baimasoft_User_LogonID"];//

                if ((UserName == Author) || UserName == TheMaster || UserName == "admin" || UserName == sReArticleAuthor)
                {
                    int iback = MyRl.DeleteRepliesInfo(RId);
                    if (iback > 0)
                    {
                        ClientScript.RegisterStartupScript(this.GetType(), "", "<script>alert('删除成功！');</script>");
                        RepliesDataBind();
                        return;
                    }
                    else
                    {
                        ClientScript.RegisterStartupScript(this.GetType(), "", "<script>alert('删除失败！');</script>");
                        return;
                    }
                }
                else
                {
                    ClientScript.RegisterStartupScript(this.GetType(), "", "<script>alert('对不起，您无此权限！');</script>");
                    return;
                }
            }
            //编辑记录
            if (e.CommandName == "Edit")
            {
                string RId = "0";
                try
                {
                    RId = e.CommandArgument.ToString().Trim();
                }
                catch
                {
                    RId = "0";
                }

                string sReArticleAuthor = myUser.GetReArticleAuthor(RId);  //当前回复者
                string UserName = (string)Session["Baimasoft_User_LogonID"]; 
                if ((UserName == Author) || UserName == TheMaster || UserName == "admin" || UserName == sReArticleAuthor)
                {

                    Response.Redirect("ReTopic_Edit.aspx?SectionID=" + SID + "&SectionName=" + SName + "&ArticleID=" + AID + "&ArticleName=" + AName + "&RepliesID=" + RId);
                }
                else 
                {
                    ClientScript.RegisterStartupScript(this.GetType(), "", "<script>alert('对不起，您无此权限！');</script>");
                    return;
                }
                
            }

        }
        catch
        {
        }
    }
    protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {

    }
    protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
    {

    }
    protected void Submit_Click(object sender, ImageClickEventArgs e)
    {
        if (AID == "")
        {
            ClientScript.RegisterStartupScript(this.GetType(), "", "<script>alert('对不起！没有找到指定主题ID，请重新登录本论坛！');</script>");
            return;
        }
        if ((string)Session["Baimasoft_User_LogonID"] == null)
        {
            ClientScript.RegisterStartupScript(this.GetType(), "", "<script>alert('对不起！您还未登陆或注册本论坛！');</script>");
            return;
        }
        else
        {
            str1 = Content.Text.ToString().Trim();
            if (str1 == "")
            {
                ClientScript.RegisterStartupScript(this.GetType(), "", "<script>alert('回复内容不能为空！');</script>");
                ArticleDataBind();
                RepliesDataBind();
                return;
            }
            else
            {
                string TheMaster; //当前讨论区版主
                TheMaster = myUser.GetMasterUser(SID);
                string[] sStr1 = TheMaster.Split(',');
                TheMaster = sStr1[0];

                string sArticleAuthor = myUser.GetArticleAuthor(AID);  //当前主题贴作者

                string UserName = (string)Session["Baimasoft_User_LogonID"]; //当前用户
                int state;
                if ((UserName == "admin") || (UserName == TheMaster) || (UserName == sArticleAuthor))
                {
                    state = 1;
                }
                else
                {
                    state = 0;
                }

                string UserIP = Request.UserHostAddress;
                string UserDNS = Request.UserHostName;
                int back = MyRl .AddReplies (SID, AID, str1, Name, UserIP, UserName, UserDNS, state);
                if (back == 1)
                {
                    if ((UserName == "admin") || (UserName == TheMaster) || (UserName == sArticleAuthor))
                    {
                        ClientScript.RegisterStartupScript(this.GetType(), "", "<script>alert('回帖成功！');location.href='Articles_List.aspx?SectionID=" + SID + "&ArticleID=" + AID + "&SectionName=" + SName + "&ArticleName=" + AName + "'</script>");
                    }
                    else
                    {
                        ClientScript.RegisterStartupScript(this.GetType(), "", "<script>alert('回帖已成功！等待版主审批通过！');location.href='Articles_List.aspx?SectionID=" + SID + "&ArticleID=" + AID + "&SectionName=" + SName + "&ArticleName=" + AName + "'</script>");
                    }
                }
                else
                {
                    ClientScript.RegisterStartupScript(this.GetType(), "", "<script>alert('回帖失败！');</script>");
                    return;
                }
            }
        }
    }
    protected void Reset_Click(object sender, ImageClickEventArgs e)
    {
        string str;
        str = "Board.aspx?SectionID=";
        str += Request.QueryString["SectionID"].ToString().Trim();
        str += "&SectionName=";
        str += Request.QueryString["SectionName"].ToString().Trim();
        Response.Redirect(str);
    }

    protected new void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        GridView1.PageIndex = e.NewPageIndex;
        this.CurrentPage = e.NewPageIndex;
        RepliesDataBind();
    }
    protected new void GridView1_DataBound(object sender, EventArgs e)
    {
        if ( MyTb .Rows.Count >0)
        {
            GridViewRow pagerRow = GridView1.BottomPagerRow;

            #region 页导航控制
            LinkButton linkBtnFirst = (LinkButton)pagerRow.Cells[0].FindControl("linkBtnFirst");
            LinkButton linkBtnPrev = (LinkButton)pagerRow.Cells[0].FindControl("linkBtnPrev");
            LinkButton linkBtnNext = (LinkButton)pagerRow.Cells[0].FindControl("linkBtnNext");
            LinkButton linkBtnLast = (LinkButton)pagerRow.Cells[0].FindControl("linkBtnLast");

            if (GridView1 .PageIndex == 0)
            {
                linkBtnFirst.Enabled = false;
                linkBtnPrev.Enabled = false;
            }
            else if (GridView1.PageIndex == GridView1.PageCount - 1)
            {
                linkBtnLast.Enabled = false;
                linkBtnNext.Enabled = false;
            }
            else if (GridView1.PageCount <= 0)
            {
                linkBtnFirst.Enabled = false;
                linkBtnPrev.Enabled = false;
                linkBtnNext.Enabled = false;
                linkBtnLast.Enabled = false;
            }
            #endregion


            string PageFormatStr = "当前第 {0} 页 共 {1} 页 共 {2} 条";

            Label pageLabel = (Label)pagerRow.Cells[0].FindControl("CurrentPageLabel");           
            if (pageLabel != null)
            {
                int currentPage = GridView1.PageIndex + 1;
                pageLabel.Text = string.Format(PageFormatStr, currentPage.ToString(), GridView1.PageCount.ToString(), MyTb.Rows.Count.ToString());
            }
        }
    }
}